package com.suke.money.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.money.suke.utils.Constants;
import com.money.suke.utils.PageModel;
import com.suke.money.mapper.LoanInfoMapper;
import com.suke.money.model.LoanInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * @Author: liush
 * @CreateTime: 2022-11-20  16:27
 * @Description: 产品业务实现类
 */
@Service(interfaceClass = LoanInfoService.class, version = "1.0.0", timeout = 20000)
@Component
public class LoanInfoServiceImpl implements LoanInfoService {

    @Autowired
    LoanInfoMapper loanInfoMapper;

    @Autowired
    RedisTemplate redisTemplate;

    //查询 历史年化收益率
    @Override
    public Double queryLoanInfoHistoryRateAvg() {
        //从redis缓存中获取 产品平均利率
        Double loanInfoHistoryRateAvg = (Double) redisTemplate.opsForValue().get(Constants.LOAN_INFO_HISTORY_RATE_AVG);
        //判断缓存中是否有数据
        if (loanInfoHistoryRateAvg == null) {
            //设置同步代码块
            synchronized (this) {
                //从redis缓存中获取 产品平均利率
                loanInfoHistoryRateAvg = (Double) redisTemplate.opsForValue().get(Constants.LOAN_INFO_HISTORY_RATE_AVG);
                //双重检测判断缓存中是否有数据
                if (loanInfoHistoryRateAvg == null) {
                    //从数据库查询 产品平均利率
                    loanInfoHistoryRateAvg = loanInfoMapper.selectLoanInfoHistoryRateAvg();
                    //将值存放到redis缓存中
                    redisTemplate.opsForValue().set(Constants.LOAN_INFO_HISTORY_RATE_AVG, loanInfoHistoryRateAvg, 30, TimeUnit.SECONDS);
                    System.out.println("(×)历史年化收益率-缓存未命中");
                } else {
                    System.out.println("(√)历史年化收益率-缓存命中");
                }
            }
        } else {
            System.out.println("(√)历史年化收益率-缓存命中");
        }
        return loanInfoMapper.selectLoanInfoHistoryRateAvg();
    }

    //首页，列表：根据产品类型和数量 查询 产品信息
    @Override
    public List<LoanInfo> queryLoanInfosByTypeAndNums(Map<String, Object> parasMap) {
        return loanInfoMapper.selectLoanInfosByTypeAndNums(parasMap);
    }

    //列表：根据类型和分页模型 查询 产品信息
    @Override
    public List<LoanInfo> queryLoanInfosByTypeAndPageModel(Integer ptype, PageModel pageModel) {
        Map<String, Object> parasMap = new HashMap<>();
        parasMap.put("ptype", ptype);
        parasMap.put("start", (pageModel.getCunPage() - 1) * pageModel.getPageSize());
        parasMap.put("content", pageModel.getPageSize());
        return loanInfoMapper.selectLoanInfosByTypeAndNums(parasMap);
    }

    //查询总记录数
    @Override
    public Long queryLoanInfoNumsByType(Integer ptype) {
        return loanInfoMapper.selectLoanInfoNumsByType(ptype);
    }

    //详情页面：根据id查询产品信息
    @Override
    public LoanInfo queryLoanInfoById(Integer loanId) {
        return loanInfoMapper.selectByPrimaryKey(loanId);
    }
}
